package org.ling.xuan.table;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.ling.xuan.entity.mo.UploadRecord;

import javax.swing.table.AbstractTableModel;
import java.util.List;

public class UploadRecordTableModel extends AbstractTableModel {
    private final List<UploadRecord> uploadRecordList;
    private final String[] columnNames = {"ID", "Modify ID", "Require ID", "Require Bank", "Requirement Description", "Add IDs", "CTD Time"};

    public UploadRecordTableModel(List<UploadRecord> uploadRecordList) {
        this.uploadRecordList = uploadRecordList;
    }

    @Override
    public int getRowCount() {
        return uploadRecordList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        UploadRecord uploadRecord = uploadRecordList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return uploadRecord.getId();
            case 1:
                return uploadRecord.getModifyId();
            case 2:
                return uploadRecord.getRequireId();
            case 3:
                return uploadRecord.getRequireBank();
            case 4:
                return uploadRecord.getRequirementDes();
            case 5:
                return String.join(",", uploadRecord.getAddIds());
            case 6:
                return DateUtil.format(uploadRecord.getCtdTime(), DatePattern.NORM_DATETIME_FORMATTER);
            default:
                return null;
        }
    }

    public void removeRow(int rowIndex) {
        uploadRecordList.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
}

